package com.crape.message.bo.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.crape.account.bo.IAccountBO;
import com.crape.common.bean.LoginUser;
import com.crape.common.enums.MessageType;
import com.crape.common.exceptions.BOException;
import com.crape.common.model.Account;
import com.crape.common.model.MsgUser;
import com.crape.common.thread.ThreadObjManager;
import com.crape.message.bo.IMsgUserBO;
import com.crape.message.dao.IMsgUserDAO;

public class MsgUserBOImpl implements IMsgUserBO {

	private IMsgUserDAO msgUserDAOImpl;
	private IAccountBO accountBOImpl;

	@Autowired
	public void setmsgUserDAOImpl(IMsgUserDAO msgUserDAOImpl) {
		this.msgUserDAOImpl = msgUserDAOImpl;
	}

	@Override
	public MsgUser getMsgUserById(Long id) throws BOException {
		return msgUserDAOImpl.getMsgUserById(id);
	}

	@Autowired
	public void setAccountBOImpl(IAccountBO accountBOImpl) {
		this.accountBOImpl = accountBOImpl;
	}

	@Override
	public List<MsgUser> getPagedMsgInfos(Long receiveid, Long sendid, Integer startIndex, Integer pageSize) throws BOException {
		if (receiveid == null || sendid == null || pageSize == null || pageSize == 0) {
			return null;
		}
		Account receiveAccount = accountBOImpl.getAccountById(receiveid);
		if (receiveAccount == null) {
			return null;
		}
		Account sendAccount = accountBOImpl.getAccountById(sendid);
		if (sendAccount == null) {
			return null;
		}
		List<MsgUser> msgList = msgUserDAOImpl.getPagedMsgInfos(receiveid, sendid, startIndex, pageSize);
		for (MsgUser msgUser : msgList) {
			msgUser.setReceiveUserName(receiveAccount.getUsername());
			msgUser.setSendUserName(sendAccount.getUsername());
		}
		return msgList;
	}

	@Override
	public Long countMsgInfos(Long receiveid, Long sendid) throws BOException {
		if (receiveid == null || sendid == null) {
			return 0L;
		}
		return msgUserDAOImpl.countMsgInfos(receiveid, sendid);
	}

	@Override
	public List<MsgUser> getPagedReceivedMsgs(Long receiveid, Integer startIndex, Integer pageSize) throws BOException {
		if (receiveid == null || startIndex == null || pageSize == null || pageSize == 0) {
			return null;
		}
		List<MsgUser> msgInboxList = msgUserDAOImpl.getPagedReceivedMsgs(receiveid, startIndex, pageSize);
		if (msgInboxList == null || msgInboxList.isEmpty()) {
			return null;
		}
		for (MsgUser msgUser : msgInboxList) {
			if (msgUser == null) {
				continue;
			}
			Account account = accountBOImpl.getAccountById(msgUser.getSendid());
			msgUser.setSendUserName(account.getUsername());
		}
		return msgInboxList;
	}

	@Override
	public Long countReceivedMsgs(Long receiveid) throws BOException {
		return msgUserDAOImpl.countReceivedMsgs(receiveid);
	}

	@Override
	public List<MsgUser> getPagedMsgsSended(Long sendid, Integer startIndex, Integer pageSize) throws BOException {
		if (sendid == null || startIndex == null || pageSize == null || pageSize == 0) {
			return null;
		}
		List<MsgUser> msgOutboxList = msgUserDAOImpl.getPagedSendedMsgs(sendid, startIndex, pageSize);
		if (msgOutboxList == null || msgOutboxList.isEmpty()) {
			return null;
		}
		for (MsgUser msgUser : msgOutboxList) {
			if (msgUser == null) {
				continue;
			}
			Account account = accountBOImpl.getAccountById(msgUser.getReceiveid());
			msgUser.setReceiveUserName(account.getUsername());
		}
		return msgOutboxList;
	}

	@Override
	public Long countSendedMsgs(Long sendid) throws BOException {
		return msgUserDAOImpl.countSendedMsgs(sendid);
	}

	@Override
	public void sendMsg(MsgUser msgUser) throws BOException {
		msgUserDAOImpl.insertMsgUser(msgUser);
	}

	@Override
	public void deleteMsgById(Long id) throws BOException {
		msgUserDAOImpl.deleteMsgUserById(id);
	}

}
